/*
 * @(#)TestsetResultType.java
 *
 *
 * ver 1.0 Oct 8, 2007 ggranum
 */
package org.testfire.model;

import java.util.Date;
import java.util.List;
import org.testfire.translator.TranslatorFactory;
import org.testfire.util.DomUtil;
import org.w3c.dom.Element;

public class TestsetResultType implements TestFireObject
{
    TestsetType parent;

    private final Element element;
    public TestsetResultType( TestsetType parent )
    {
        this.parent = parent;
        element = parent.getElement().getOwnerDocument().createElement( "tf:TestsetResult" );
        parent.getElement().appendChild( element );
    }

    public TestsetResultType( TestsetType parent, Element element )
    {
        this.parent = parent;
        this.element = element;
    }

    public void setParent( TestFireObject parent )
    {
        this.parent = (TestsetType)parent;
    }

    public TestFireObject getParent()
    {
        return this.parent;
    }

    public Object translate( TestsetType testset )
    {
        return TranslatorFactory.translate( testset, this );
    }

    public boolean isCompileFailed()
    {
        return Boolean.valueOf( (element.getAttribute( "compileFailed" )) );
    }

    public void setCompileFailed( boolean compileFailed )
    {
        element.setAttribute( "compileFailed", String.valueOf( compileFailed ) );
    }

    public int getFailureCount()
    {
        return Integer.parseInt( element.getAttribute( "failureCount" ) );
    }

    public void setFailureCount( int failureCount )
    {
        element.setAttribute( "failureCount", String.valueOf( failureCount ) );
    }

    public int getIgnoreCount()
    {
        return Integer.parseInt( element.getAttribute( "ignoreCount" ) );
    }

    public void setIgnoreCount( int ignoreCount )
    {
        element.setAttribute( "ignoreCount", String.valueOf( ignoreCount ) );

    }

    public String getMessages()
    {
        Element el = DomUtil.getChildElementByTagName( "tf:messages", element );
        return el != null ? el.getTextContent() : "";
    }

    public void setMessages( String messages )
    {
        Element el = DomUtil.getChildElementByTagName( "tf:messages", element );
        if ( el == null )
        {
            el = element.getOwnerDocument().createElement( "tf:messages" );
            element.appendChild( el );
        }
        el.setTextContent( messages );
    }

    public int getRunCount()
    {
        return Integer.parseInt( element.getAttribute( "runCount" ) );
    }

    public void setRunCount( int runCount )
    {
        element.setAttribute( "runCount", String.valueOf( runCount ) );
    }

    public Date getRunDate()
    {
        return DomUtil.toJavaDate( element.getAttribute( "runDate" ) );
    }

    public void setRunDate( Date runDate )
    {
        element.setAttribute( "runDate", DomUtil.toXMLDateTime( runDate ) );
    }

    public int getTimeElapsed()
    {
        return Integer.valueOf( element.getAttribute( "timeElapsed" ) );
    }

    public void setTimeElapsed( int timeElapsed )
    {
        element.setAttribute( "timeElapsed", String.valueOf( timeElapsed ) );
    }

    public boolean isWasSuccessful()
    {
        return Boolean.valueOf( (element.getAttribute( "wasSuccessful" )) );
    }

    public void setWasSuccessful( boolean wasSuccessful )
    {
        element.setAttribute( "wasSuccessful", String.valueOf( wasSuccessful ) );
    }

    /**
     * Convenience method for locating TestResults that were run under this TestSetResult.
     * @return A list of TestResults for this TestsetResult.
     */
    public List<TestResultType> getTestResults()
    {
        return this.parent.getTestResults( this );
    }
}

// eof: TestsetResultType.java